package com.yyy.service;

import com.yyy.dao.RecordDao;
import com.yyy.entity.Record;
import com.yyy.gui.page.SpendPage;
import com.yyy.util.DateUtil;

import java.util.List;

/**
 * @Author：yyy
 * @Package：com.yyy.service
 * @Project：HuTuBiill
 * @name：SpendService
 * @Date：2024/11/10 10:32
 * @Filename：SpendService
 */
public class SpendService {
    public SpendPage getSpendPage(){
        RecordDao dao=new RecordDao();
        List<Record> monthRecords = dao.listMonth();//本月消费
        List<Record> todayRecords = dao.listToday();//今日消费
        int thisMonthDay= DateUtil.thisMonthTotalDay();//本月总天数

        int monthSpend=0;//月消费
        int todaySpend=0;//日消费
        int avgSpendPerDay=0;//日均消费
        int monthAvailable=0;//月可消费
        int dayAvailable=0;//日可消费
        int monthLeftDay=DateUtil.thisMonthLeftDay();//月剩余天数
        int usagePercentage=0; //消费百分比
        int monthBudget=new ConfigService().getIntBudget();//等待从配置表中获取

        //统计本月消费
        for(Record record:monthRecords){
            monthSpend+= record.getSpend();
        }
        //统计今日消费
        for(Record record:todayRecords){
            todaySpend+= record.getSpend();
        }
        //计算日均消费
        avgSpendPerDay=monthSpend/(thisMonthDay-monthLeftDay);
        //计算本月剩余
        monthAvailable=monthBudget-monthSpend;

        //计算日均可用
        dayAvailable=monthAvailable/monthLeftDay;

        //计算使用比例
        usagePercentage=monthSpend*100/monthBudget;



        return new SpendPage(monthSpend,todaySpend,avgSpendPerDay,monthAvailable,dayAvailable,monthLeftDay,usagePercentage);
    }
}
